package io.xccit.r;

import lombok.Data;

/**
 * @author CH_ywx
 * @version 1.0
 * @create 2024/10/13
 * <p>统一返回结果</p>
 */
@Data
public class XResult<T> {
    private Integer code;
    private String message;
    private T data;

    private XResult() {}
    public XResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
    private XResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 请求成功
     * @param data 数据
     * @return 响应结果
     * @param <T> 数据类型
     */
    public static <T> XResult<T> success(T data) {
        return new XResult<>(HttpStatusCode.SUCCESS.getCode(), HttpStatusCode.SUCCESS.getMessage(), data);
    }

    /**
     * 请求成功
     * @return 响应结果
     * @param <T> 数据类型
     */
    public static <T> XResult<T> success() {
        return new XResult<>(HttpStatusCode.SUCCESS.getCode(), HttpStatusCode.SUCCESS.getMessage());
    }

    /**
     * 请求成功
     * @param httpStatusCode 请求状态
     * @return 响应结果
     * @param <T> 数据类型
     */
    public static <T> XResult<T> success(HttpStatusCode httpStatusCode) {
        return new XResult<>(httpStatusCode.getCode(), httpStatusCode.getMessage());
    }

    /**
     * 请求成功
     * @param httpStatusCode 请求状态
     * @param data 数据
     * @return 响应结果
     * @param <T> 数据类型
     */
    public static <T> XResult<T> success(HttpStatusCode httpStatusCode, T data) {
        return new XResult<>(httpStatusCode.getCode(), httpStatusCode.getMessage(),data);
    }

    /**
     * 请求失败
     * @param message 消息
     * @return 响应结果
     * @param <T> 数据类型
     */
    public static <T> XResult<T> error(String message) {
        return new XResult<>(HttpStatusCode.ERROR.getCode(), message);
    }

    /**
     * 请求失败
     * @param code 状态码
     * @param message 消息
     * @return 响应结果
     * @param <T> 数据类型
     */
    public static <T> XResult<T> error(Integer code, String message) {
        return new XResult<>(code, message);
    }

    /**
     * 请求失败
     * @param httpStatusCode 请求状态
     * @return 响应结果
     * @param <T> 数据类型
     */
    public static <T> XResult<T> error(HttpStatusCode httpStatusCode) {
        return new XResult<>(httpStatusCode.getCode(), httpStatusCode.getMessage());
    }

    /**
     * 请求失败
     * @param httpStatusCode 请求状态
     * @param data 数据
     * @return 响应结果
     * @param <T> 数据类型
     */
    public static <T> XResult<T> error(HttpStatusCode httpStatusCode,T data) {
        return new XResult<>(httpStatusCode.getCode(), httpStatusCode.getMessage(),data);
    }
}
